package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.goods.entity.category.SalesCategory;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.category.SalesCategoryMapper;
import com.hoshiicloud.goods.service.SalesCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 营销类目 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-17
 */
@Slf4j
@Service
public class SalesCategoryServiceImpl extends ServiceImpl<SalesCategoryMapper, SalesCategory> implements SalesCategoryService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * @param id
     * @return com.hoshiicloud.goods.entity.SalesCategory
     * @description: 根据id查询营销类目
     */
    @Override
    @Cacheable(cacheNames = "SalesCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "商品管理-营销类目-查询营销类目 ")
    public SalesCategory getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增营销类目
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目-新增营销类目")
    public boolean save(SalesCategory entity) {

        // 主键ID生成
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改营销类目
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-修改营销类目")
    public boolean updateById(SalesCategory entity) {

        // 更新数据
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
     * @param queryWrapper
     * @return IPage
     * @description: 分页查询营销类目
     */
    @Override
    @Logable(authName = "商品管理-营销类目管理-分页查询营销类目")
    @Cacheable(cacheNames = "SalesCategory", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5(#root.targetClass.simpleName+':'+#root.methodName+':'+#key)")
    public IPage getPage(IPage page, Wrapper queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-删除营销类目")
    public boolean deleteById(Long id) {
        // 根据id查询数据
        SalesCategory entity = this.getById(id);
        if (entity == null) {
            log.error("删除营销类目异常，无法找到营销类目记录");
            throw new ServiceException("删除营销类目异常，无法找到营销类目记录");
        }

        //进行删除操作
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Override
    @Cacheable(cacheNames = "SalesCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品管理-营销类目管理-根据id集合查询营销类目")
    public List<SalesCategory> getByIds(List<Long> ids, String key) {
        return baseMapper.selectBatchIds(ids);
    }
}
